﻿// GameDlg.cpp : 茄君匡숭
//

#include "stdafx.h"
#include "GameDlg.h"
#include "afxdialogex.h"
#include "resource.h"
#include "CppSQLite3.h"

using namespace std;
HWND   g_main_hwnd = NULL;
CGameDlg * g_main_cwnd = NULL;

// CGameDlg 뚤뺐움

IMPLEMENT_DYNAMIC(CGameDlg, CDialogEx)

CGameDlg::CGameDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_GAME_DIALOG, pParent)
{

}

CGameDlg::~CGameDlg()
{
}

void CGameDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CGameDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON1, &CGameDlg::OnBnClickedButton1)
END_MESSAGE_MAP()


// CGameDlg 句口뇹잿넋埼
unsigned WINAPI SubThread1(PVOID pParam)
{
	g_dm->SetSimMode(1);
	long hwnd = g_dm->FindWindow(L"MapleStoryClass", L"MapleStory");

	if (g_dm->GetBindWindow() == 0) {
		g_dm->BindWindowEx(hwnd, L"normal", L"normal", L"normal", L"", 0);
	}

	g_dm->delay(5000);
	for (size_t i = 0; i < 20; i++)
	{
		Sleep(200);
		g_dm->KeyDown(16);
		g_dm->delay(200);
		g_dm->KeyUp(16);
		Sleep(200);
		
	}
/*
	g_dm->KeyDown(16);
	g_dm->KeyDown(16);
	g_dm->delay(10000);
	g_dm->KeyUp(16);
	g_dm->KeyUp(16);
	g_dm->delay(3000)*/;
	return 0;
}
unsigned WINAPI MainThread(PVOID pParam)
{

	long hwnd = g_dm->FindWindow(L"MapleStoryClass", L"MapleStory");

	int ret = g_dm->BindWindowEx(hwnd, L"normal", L"normal", L"normal", L"", 0);
	g_dm->SetWindowState(hwnd, 12);
	long x, y;
	CString str;
	while (true)
	{
		
		str += ".";
		g_main_cwnd->AppendText(L"等待交易中"+str);
		//꿴冷쌈肝눗왯
		//更改
		CString taskStr = L"loginOkWnd|closeWnd|closeWnd1|closeWnd4";
		runScriptList(taskStr);
		//查找接受按钮
		g_dm->FindPic(578, 638, 600, 680, L"jieshou.bmp", L"050505", 0.95, 0, &x, &y);
		if (x > 0)//找到接受
		{
			str = L"";
			g_main_cwnd->AppendText(L"点击接受");
			g_dm->MoveTo(x + 5, y - 8);
			g_dm->delay(500);
			g_dm->LeftClick();
			Sleep(1000);
		}
		long jyWidth, jyHeight;//交易窗口定位
		//查找交易窗口
		Sleep(500); //Sleep(1000);

		g_dm->FindPic(245, 144, 784, 365, L"jyWnd.bmp", L"050505", 0.95, 0, &jyWidth, &jyHeight);
		if (jyWidth > 0)//找到交易窗口
		{
			g_main_cwnd->AppendText(L"交易窗口已打开");
			g_dm->MoveTo(1000, RandomNumber(100, 150));//移动鼠标
			//查找背包是否打开
			Sleep(1000);
			g_dm->FindPic(1, 12, 239, 521, L"wupinWnd.bmp", L"050505", 0.90, 0, &x, &y);
			if (x > 0) {
				g_main_cwnd->AppendText(L"打开物品消耗栏");
				g_dm->MoveTo(x + 10, y + 25);
				Sleep(1000);
				g_dm->LeftClick();
				Sleep(1000);
				g_dm->MoveTo(1000, RandomNumber(100, 150));
			}
			//查找窗口内的药品是否存在
			for (size_t i = 0; i < 10; i++)
			{
				//找到交易窗口 ， 查找交易窗口是否存在药品
				g_dm->FindPic(jyWidth+40, jyHeight+80, jyWidth + 40 + 143, jyHeight + 80 + 100, L"yaoshui.bmp", L"303030", 0.80, 0, &x, &y);

				if (x == -1)//交易栏不存在药水{
				{
					//查找背包内的药品坐标
					g_dm->FindPic(1, 12, 239, 521, L"yaoshui.bmp", L"202020", 0.90, 0, &x, &y);
					if (x > 0) {
						g_main_cwnd->AppendText(L"找到药水位置");
						Sleep(1000);
						g_dm->MoveTo(x + 20, y + 10);
						Sleep(500);
						g_dm->LeftClick();
						Sleep(500);
						g_dm->MoveTo(jyWidth -79+ 5 * 35, jyHeight+144);//移动药水
						g_main_cwnd->AppendText(L"移动药水到交易栏");
						Sleep(1000);
						g_dm->LeftClick();
						g_dm->MoveTo(900, RandomNumber(100, 150));
					}
					Sleep(RandomNumber(1000, 1500));
					g_dm->FindPic(20, 156, 783, 526, L"okNumWnd.bmp", L"050505", 0.90, 0, &x, &y);
					if (x> 0) {
						g_main_cwnd->AppendText(L"找到数量窗口");
						Sleep(1000);
						g_dm->KeyPressStr(L"30", 2000);
						Sleep(1000);
						g_dm->KeyPress(13);
						Sleep(1000);

						for (size_t k = 0; k < 5; k++)
						{
							g_dm->FindPic(jyWidth + 40, jyHeight + 80, jyWidth + 40 + 143, jyHeight + 80 + 100, L"yaoshui.bmp", L"303030", 0.80, 0, &x, &y);
							Sleep(500);
							if (x > 0) {
								g_main_cwnd->AppendText(L"药水已经移动到交易栏点击交易确认");
							}
							break;
						}
						if (x == -1) {
							g_dm->FindPic(245, 144, 784, 365, L"jyWnd.bmp", L"050505", 0.95, 0, &jyWidth, &jyHeight);
							if (jyWidth ==-1)//找到交易窗口
							{
								break;
							}
							g_main_cwnd->AppendText(L"物品栏未找到药水重新交易");
							continue;
						}
						//确认交易
						Sleep(500);
						g_dm->MoveTo(jyWidth + 40, jyHeight + 10);
						g_dm->LeftClick();
						Sleep(500);
						g_main_cwnd->AppendText(L"交易成功");
						Sleep(500);
						g_main_cwnd->SetDlgItemTextW(IDC_EDIT1, L"");
						//g_dm->KeyPress(13);
						//Sleep(500);
						//g_dm->KeyPress(13);
						//Sleep(2000);
						//g_dm->KeyPress(13);
						break;
					}
					Sleep(500);
				}

				if (x > 0) {
					break;
				}

			}
			//jyWidth = jyWidth - 79;
			//jyHeight = jyHeight + 144;
		}

		g_dm->FindPic(372, 288, 649, 453, L"okWnd.bmp|okWnd1.bmp", L"050505", 0.95, 0, &x, &y);
		if (x > 0)//找到接受
		{
			g_dm->MoveTo(x + 5, y+8);
			g_dm->delay(500);
			g_dm->LeftClick();

		}
		
		
	}
	/*
	g_dm->KeyDown(16);
	g_dm->KeyDown(16);
	g_dm->delay(10000);
	g_dm->KeyUp(16);
	g_dm->KeyUp(16);
	g_dm->delay(3000)*/;
	return 0;
}


void CGameDlg::OnBnClickedButton1()
{
	// TODO: 瞳늪警속왠숭繫列뇹잿넋埼덜쯤
	GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE);
	_beginthreadex(0, 0, MainThread, NULL, 0, 0);
}



BOOL CGameDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  瞳늪警속띨棍돨놓迦뺏

	CppSQLite3DB db;
	CFileFind fFind;
	g_main_cwnd = this;
	if (fFind.FindFile(_T("C:\\MobileGame\\DMTool.db")))
	{
		db.open("C:\\MobileGame\\DMTool.db", "ppo00123");
		CppSQLite3Query query = db.execQuery("SELECT * FROM ocrDetail where status='0' order by id desc");
		//ocrCmp.RemoveAll();
		ocrMap.clear();
		while (!query.eof())
		{

			OcrInfo ocrInfo;
			ocrInfo.sName = query.fieldValue("name");
			ocrInfo.sOcrText = query.fieldValue("ocrText");
			ocrInfo.sAction = query.fieldValue("action");
			ocrInfo.nCount = atoi(query.fieldValue("count") == NULL ? 0 : query.fieldValue("count"));
			ocrInfo.nDelay = atoi(query.fieldValue("delay") == NULL ? 0 : query.fieldValue("delay"));
			ocrInfo.sEnlarge = query.fieldValue("enlarge");
			ocrInfo.sInStr = query.fieldValue("instr");
			ocrInfo.sOffset = query.fieldValue("offset");
			ocrInfo.sPoint = query.fieldValue("point");
			ocrInfo.sType = query.fieldValue("type");
			ocrInfo.sDescribe = query.fieldValue("describe");
			ocrMap[CString(query.fieldValue("name"))] = ocrInfo;
			query.nextRow();
		}
		db.close();
	}
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 嫩끽: OCX 橄昑女壇럿쀼 FALSE
}
void CGameDlg::AppendText(CString str)
{
	((CEdit*)GetDlgItem(IDC_EDIT1))->SetSel(GetDlgItem(IDC_EDIT1)->GetWindowTextLength(), GetDlgItem(IDC_EDIT1)->GetWindowTextLength());
	((CEdit*)GetDlgItem(IDC_EDIT1))->ReplaceSel(str + L"\r\n");
}



